package com.dengcj.epl.service.util;

import cn.hutool.db.Entity;
import com.dengcj.epl.dao.DB;
import com.dengcj.util.CjDateTimeUtil;
import com.dengcj.util.CjSqlUtil;
import com.dengcj.util.CjUtil;
import com.dengcj.util.LogCj;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 表单建模工具类
 */
public class ModeUtil {

    LogCj logCj = new LogCj();
    /**
     * 新增数据
     * @param creator 创建用户
     * @param tablename 表名
     * @param mainFormFieldList 主表数据List
     * @return 新增数据的ID
     */

    public List<Integer> addMainForm(int creator,String tablename,List<Map<String,Object>> mainFormFieldList){
        logCj.setLogFileName("Mode_Error");
        int modeid = getModeIdByTablename(tablename);
        if(modeid < 1){
            logCj.log("modeid:"+modeid+"获取表名失败");
            return new ArrayList<>();
        }
        int formid = getFormidByModeid(modeid);

        return addMainForm(creator,modeid,formid,tablename,mainFormFieldList);
    }
    /**
     * 以管理员身份新增数据
     * @param tablename 表名
     * @param mainFormFieldList 主表数据List
     * @return 新增数据的ID
     */

    public List<Integer> addMainForm(String tablename,List<Map<String,Object>> mainFormFieldList){
        logCj.setLogFileName("Mode_Error");
        int modeid = getModeIdByTablename(tablename);
        if(modeid < 1){
            logCj.log("modeid:"+modeid+"获取表名失败");
            return new ArrayList<>();
        }
        int formid = getFormidByModeid(modeid);

        return addMainForm(1,modeid,formid,tablename,mainFormFieldList);
    }
    
    public int addMainForm(int creator,int modeid,int formid,String tablename,Map<String,Object> mainFormFieldMap){

        int billid = -1;

        CjDateTimeUtil dateTimeUtil = new CjDateTimeUtil();

        //添加建模默认的字段
        mainFormFieldMap.put("formmodeid",modeid);
        mainFormFieldMap.put("modedatacreater",creator);
        mainFormFieldMap.put("modedatacreatertype",0);
        mainFormFieldMap.put("modedatacreatedate",dateTimeUtil.getDate());
        mainFormFieldMap.put("modedatacreatetime",dateTimeUtil.getTime());

        //组装插入SQL
        String insertSql = CjSqlUtil.getInsertSql(tablename,mainFormFieldMap);

        try {
            DB db = new DB();
            billid = db.insert(insertSql).intValue();

            if(billid < 0){
                logCj.log("ModeUtil-insert faild, sql:"+insertSql);
            }
        }catch (Exception e){
            logCj.log(e.getMessage());

            e.printStackTrace();
        }

        return billid;
    }

    
    public List<Integer> addMainForm(int creator, int modeid, int formid, String tablename, List<Map<String,Object>> mainFormFieldList){

        List<Integer> newIdList = new ArrayList<>();

        int colSize = mainFormFieldList.size();
        for(int i=0; i<colSize ;i++) {
            Map<String, Object> mainFormFieldMap = mainFormFieldList.get(i);

            int billid = -1;
            mainFormFieldMap = addModeDefaultCol(mainFormFieldMap, modeid, creator);

            //组装插入SQL
            String insertSql = CjSqlUtil.getInsertSql(tablename, mainFormFieldMap);

            try {
                DB db = new DB();
                billid = db.insert(insertSql).intValue();

                if(billid < 0){
                    logCj.log("ModeUtil-insert faild, sql:"+insertSql);
                } else {
                    newIdList.add(billid);
                }
            } catch (Exception e) {
                logCj.log(e.getMessage());
                e.printStackTrace();
                break;
            }
        }

        return newIdList;
    }

    
    public List<Integer> addMainForm(String creatorKey, int modeid, int formid, String tablename, List<Map<String,Object>> mainFormFieldList){

        List<Integer> newIdList = new ArrayList<>();

        List<Integer> newIdCreatorList = new ArrayList<>();

        int colSize = mainFormFieldList.size();
        for(int i=0; i<colSize ;i++) {
            Map<String, Object> mainFormFieldMap = mainFormFieldList.get(i);

            int creator = CjUtil.getIntVal(CjUtil.null2Str(mainFormFieldMap.get(creatorKey)), -1);

            if (creator < 0) {
                logCj.log("mainFormFieldMap中未找到" + creatorKey + "的key或者该项的值不合法");
                break;
            }

            int billid = -1;
            //添加建模默认的字段
            mainFormFieldMap = addModeDefaultCol(mainFormFieldMap, modeid, creator);

            //组装插入SQL
            String insertSql = CjSqlUtil.getInsertSql(tablename, mainFormFieldMap);

            try {
                DB db = new DB();
                billid = db.insert(insertSql).intValue();

                if (billid < 0) {
                    logCj.log("ModeUtil-insert faild, sql:" + insertSql);
                } else {
                    newIdList.add(billid);
                }
            } catch (Exception e) {
                logCj.log(e.getMessage());
                e.printStackTrace();
                break;
            }
        }

        return newIdList;
    }

    /**
     * 往传入的字段map中添加建模的默认字段
     * @param colMap
     * @param modeId
     * @param creator
     * @return
     */
    
    public Map<String,Object> addModeDefaultCol(Map<String,Object> colMap, int modeId, int creator){
        CjDateTimeUtil dateTimeUtil = new CjDateTimeUtil();
        colMap.put("formmodeid", modeId);
        colMap.put("modedatacreater", creator);
        colMap.put("modedatacreatertype", 0);
        colMap.put("modedatacreatedate", dateTimeUtil.getDate());
        colMap.put("modedatacreatetime", dateTimeUtil.getTime());

        return colMap;
    }


    /**
     * 根据查询页面的id获取对应所属模块的id
     * @param queryid
     * @return
     */
    public static int getQueryModeId(int queryid) {
        DB db = new DB();
        Entity entity = db.queryOne("select modeid from mode_customsearch where id=?", queryid);
        return CjUtil.getIntVal(entity.getStr("modeid"), -1);
    }

    /**
     * 根据模块id获取对应表单的数据库表名
     * @param modeid
     * @return
     */
    public String getTablenameByModeid(int modeid) {
        DB db = new DB();
        Entity entity = db.queryOne("select tablename from workflow_bill a inner join modeinfo b on a.id=b.formid where b.id=?", modeid);
        return entity.getStr("tablename");
    }

    /**
     * 根据模块id获取对应表单的数据库表名
     * @param tablename
     * @return
     */
    public int getModeIdByTablename(String tablename) {
        DB db = new DB();
        Entity entity = db.queryOne("select b.id from workflow_bill a inner join modeinfo b on a.id=b.formid where a.tablename=?", tablename);
        return entity == null ? -1 : entity.getInt("id");
    }

    /**
     * 根据模块id获取对应表单id
     * @param modeid
     * @return
     */
    public int getFormidByModeid(int modeid) {
        DB db = new DB();
        Entity entity = db.queryOne("select a.id from workflow_bill a inner join modeinfo b on a.id=b.formid where b.id=?", modeid);
        return CjUtil.getIntVal(entity.getStr("id"), -1);
    }

    /**
     * 根据查询id获取对应表单的数据库表名
     * @param queryid
     * @return
     */
    public String getTablenameByQueryid(int queryid) {
        DB db = new DB();
        Entity entity = db.queryOne("select tablename from workflow_bill a inner join mode_customsearch b on a.id=b.formid where b.id=?", queryid);
        return entity.getStr("tablename");
    }
}
